version 15

*** set the folder with the data
*cd "C:\myfolder"

use  "reselection_dataforanalysis.dta", clear

global ctrls = "leadership2 age female2  replace lag_logipcomp  lag_ListRank_First lag_ListRank_Rel lag_Ord_First lag_Ord_Rel i.ElYearLead"

est clear
logit Demotion_d i.ter_* $ctrls , 	cluster (IDptyLag) 
est store l

mlogit move3 i.ter_* $ctrls ,  base(1)	cluster (IDptyLag) , if lag_ListRank_First == 0
est store m

* export regression table
*esttab l m using "regmodels.rtf" , b(2) se(2) star  obslast mtitles compress  replace

* 3 outcomes * 3 activities * 3 terciles 
margins i.ter_* 

* write margins results to file for plotting in R
preserve
clear
mat out = r(table)
svmat out
drop in 2/4
drop in 4/6
xpose, clear
ren v1-v3 (mean lower upper)
gen outcome = -1
replace outcome = 0 in 10/18
replace outcome = 1 in 19/27
gen act = "Speeches"
replace act = "Questions" in 4/6
replace act = "Questions" in 13/15
replace act = "Questions" in 22/24
replace act = "Rapports" in 7/9
replace act = "Rapports" in 16/18 
replace act = "Rapports" in 25/27
gen tmp = _n
byso outcome act (tmp): gen terc = _n
drop tmp
save "reselection_dataforgraph.dta", replace
restore




